package com.cuiweiyou.bluemesh.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cuiweiyou.bluemesh.R;
import com.cuiweiyou.bluemesh.bluetooth.BluetoothMaster;
import com.cuiweiyou.bluemesh.model.EventDeviceModel;
import com.cuiweiyou.bluemesh.model.LightModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GroupInitFragment extends Fragment {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private String groupName;

    private DevicesInitAdapter devicesInitAdapter;

    public static GroupInitFragment getInstance(String name) {
        GroupInitFragment fragment = new GroupInitFragment();
        Bundle bundle = new Bundle();
        bundle.putString("name", name);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getArguments();
        groupName = bundle.getString("name");

        BluetoothMaster.getInstance().scan(groupName);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        View view = inflater.inflate(R.layout.fragment_item_initgroup, container, false);
        ButterKnife.bind(this, view);

        initView();

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onDeviceEvent(EventDeviceModel model) { // 收到设备列表刷新
        flushDeviceList();  // 收到设备列表刷新
    }

    private void initView() {
        devicesInitAdapter = new DevicesInitAdapter();
        devicesInitAdapter.setOnDeviceLongClickListener(onDeviceLongClickListener);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(devicesInitAdapter);
    }

    private DevicesInitAdapter.OnDeviceLongClickListener onDeviceLongClickListener = new DevicesInitAdapter.OnDeviceLongClickListener() {
        @Override
        public void onDeviceLongClick(LightModel model) {
            setNickName(model);
        }
    };

    private void setNickName(final LightModel light) {
        EditTextDialog.createDialog(getActivity(), new EditTextDialog.OnEditTextDialogDismissListener() {
            @Override
            public void onEditTextDialogDismiss(String nickname) {
                //NicknameSqlUtil.getInstance().insert(light, nickname); // 长按修改昵称后
                light.setNickname(nickname);
                flushDeviceList(); // 更改名称
            }
        });
    }

    private void flushDeviceList() {
        List<LightModel> deviceResultList = BluetoothMaster.getInstance().getDeviceListByGroup(groupName);
        if (null != devicesInitAdapter) {
            devicesInitAdapter.updateDevices(deviceResultList);
        }
    }
}
